package gui;

import com.google.common.eventbus.Subscribe;
import config.AppConfig;
import config.ConfigWatch;
import eventbus.PageEventBus;
import eventbus.events.AppReloadEvent;
import eventbus.events.ConfigChangedEvent;
import gui.panels.*;
import network.websocket.WSServer;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

public class MainPage extends JFrame {
    private static final String BASE_IMG_PATH = "./res/imgs/";
    private static final int WIDTH = 1020;
    private static final int HEIGHT = 651;
    private static final int LOCATION_X = 500;
    private static final int LOCATION_Y = 200;
    MainPanel mainPanel = new MainPanel(this);
    boolean isClose = false;

    public MainPage() {
        super("主页面");
        init();
        ImageIcon imageIcon = new ImageIcon(BASE_IMG_PATH + "logo_128_128.jpg");
        setIconImage(imageIcon.getImage());
        PageEventBus.subscribe(this);
        SubPanel.registerBackBtnListener(e -> {
            setContentPane(mainPanel);
            setTitle("主页面");
        });
        mainPanel.addSubPanel("./res/imgs/onlineUser.png", "在线用户", "实时监控用户在线情况", OnlineUserSubPanel.class);
        mainPanel.addSubPanel("./res/imgs/userMgr.png", "用户管理", "用户管理", UserManagerSubPanel.class);
        mainPanel.addSubPanel("./res/imgs/messageMgr.png", "聊天消息", "聊天消息查看", ChatMessageSubPanel.class);
        mainPanel.addSubPanel("./res/imgs/log.png", "操作记录", "操作记录", OperationRecordSubPanel.class);
        mainPanel.addSubPanel("./res/imgs/flowMonitoring.png", "流量监控", "流量监控", FlowMonitoringSubPanel.class);
        mainPanel.addSubPanel("./res/imgs/systemConfig.png", "系统设置", "系统设置", SystemConfigSubPanel.class);
        this.setContentPane(mainPanel);
        this.setBounds(LOCATION_X, LOCATION_Y, WIDTH, HEIGHT);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("关闭");
                System.exit(0);
            }
        });
    }

    void init() {
        String themeName = AppConfig.OtherConfig.getTheme();
        String fontName = AppConfig.OtherConfig.getFontFamily();
        try {
            //更改全局字体
            FontUIResource fontRes = new FontUIResource(new FontUIResource(fontName, Font.PLAIN, 14));
            for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
                Object key = keys.nextElement();
                Object value = UIManager.get(key);
                if (value instanceof FontUIResource) {
                    UIManager.put(key, fontRes);
                }
            }
            //更改风格更好看一些
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if (themeName.equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /**
     * 释放当前主界面并重新创建主界面,ws服务重启
     */
    void restart() {
        if (!isClose) {
            this.dispose();
            new MainPage();
            WSServer.Instance.restartServer();
            ConfigWatch.disinhibition();
            isClose = true;
        }
    }

    @Subscribe
    public void reload(AppReloadEvent e) {
        restart();
    }

    @Subscribe
    public void configChanged(ConfigChangedEvent e) {
        if (isClose) {
            return;
        }
        int n = JOptionPane.showConfirmDialog(null, "检测到配置改变是否重启应用?");
        if (n == JOptionPane.OK_OPTION) {
            restart();
        }

    }
}
